<?php
namespace app\index\model;
use think\Db;
use think\Model;
use traits\model\SoftDelete;



class Users extends Model{

    Use SoftDelete;
    protected $deleteTime="del_time";
    protected $autoWriteTimestamp=true;
    protected $createTime='reg_time';
    protected $updateTime='update_time';

    protected $insert=['vip'=>'1','status'=>'0'];

    protected function getSexAttr($value){
        $sex=\think\Config::get('sex');
        return $sex[$value];
    }
    protected function getVipAttr($value){
        $vip=\think\Config::get('vip');
        return $vip[$value];
    }
    protected function getStatusAttr($value){
        $status=\think\Config::get('status');
        return $status[$value];
    }
    protected function getStatusNameAttr($value,$data){
            $arr=['激活','关闭'];
        return $arr[$data['status']];
    }


    protected function setPasswordAttr($value){
        $password=md5($value);
        return $password;
    }
    public static function userList($data=[],$query=[],$page=true){
        $model=self::alias('u')->join('app_avatar a','id=user_id','LEFT');
        if (isset($data['username'])) {
            $model->where('username', 'like', '%' . $data['username'] . '%');
        }
        if (isset($data['nickname'])) {
            $model->where('nickname', 'like', '%' . $data['nickname'] . '%');
        }
        if (isset($data['vip'])) {
            $model->where('vip', 'in', $data['vip']);
        }
        //生日
        if (isset($data['birthday1'])) {
            $model->whereTime('birthday', '>', $data['birthday1']);
        }
        if (isset($data['birthday2']) and $data['birthday2']!=0) {
            $model->whereTime('birthday', '<', $data['birthday2']);
        }
        //注册时间
        if (isset($data['reg_time1'])) {
            $model->whereTime('reg_time', '>', $data['reg_time1']);
        }
        if (isset($data['reg_time2'])) {
            $model->whereTime('reg_time', '<', $data['reg_time2']);
        }
        //性别单选
        if(isset($data['sex'])){
            $model->where('sex',$data['sex']);
        }





        //头像复选
        if (isset($data['pic_url'][0]) and $data['pic_url'][0]==2) {
            $model->where('pic_url','=','');
        }
        if (isset($data['pic_url'][0]) and $data['pic_url'][0]==1) {
            $model->where('pic_url','<>','');
        }





//点击排序
        if ($data['order']) {
            $model->order(['reg_time' => $data['order']]);
        } else {
            $model->order(['id' => 'desc']);
        }


        if($page){
            $request=$model->paginate(3,false,['query' => $query]);
        }else{
            $request=$model->select();
        }
        $list=$request;

        return $list;
    }
    public static function huishou($data=[],$query=[],$page=true){
        $model=self::onlyTrashed()->alias('u')->join('app_avatar a','id=user_id','LEFT');


        if($page){
            $request=$model->paginate(5,false,['query' => $query]);
        }else{
            $request=$model->select();
        }
        $list=$request;


        return $list;

    }
    public static function login($post=[]){
        $info=self::where('username',$post['username'])->where('password',md5($post['password']))->find();
        return $info;
    }

    public static function loginn($post=[]){

        $info=self::where('username',$post['username'])->where('password',md5($post['psw']))->find();

        return $info;
    }



    public static function getUser($username = NUll,$userId = Null){

        if(empty($userId) and empty($username)){
            return false;
        }

        $field = [
            'id','username','password','mobile','nickname','email','sex','status',
            'vip','birthday','reg_time','reset_time','a.pic_url','a.thumb_url',
        ];

        $model = self::alias('u')->field($field)->join(['app_avatar'=>'a'],'u.id = a.user_id','LEFT');

        if(!empty($userId)){
            $model->where('id',$userId);
        }

        if(!empty($username)){
            $model->where('username',$username);
        }

        return $model->find();
    }




    public function adduser($data=[]){
        try {
            if (empty($data)) {
                $this->error = "不能为空";
                return false;
            }

            Db::startTrans();
            $this->allowField(true)->save($data);
            $id=$this->getLastInsID();

            $ava=new Avatar();
            $data['user_id']=$id;
            $ava->addpic($data);
//                Db::table('app_avatar')->insert([
//                    'user_id'=>$id,
//                    'pic_url'=>$data['pic_url'],
//                    'thumb_url'=>$data['thumb_url']
//                ],true);

            $this->commit();

            return $id;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }





    public function edit_user($data=[]){
        try{
            if(empty($data)){
                $this->error="不能为空";
                return false;
            }

                return $this->isUpdate(true)->allowField(true)->save($data);


        }catch(\Exception $e){
            $this->error=$e->getMessage();
            return false;
        }
    }


    public static function log($data=[]){
        $rt=self::field(true);
        $data['psw']=md5($data['psw']);
        $a=$rt->where('password',$data['psw'])
            ->find();
        return $a;
    }

    public static function kuai($data=[]){
        $rt=self::field(true);

        $a=$rt->where('md55',$data)
            ->find();
        return $a;


    }
    public function editpsw($data=[]){
        try{
            if(empty($data)){
                $this->error="不能为空";
                return false;
            }
            return $this->isUpdate(true)->allowField(true)->save($data);
        }catch(\Exception $e){
            $this->error=$e->getMessage();
            return false;
        }
    }
    public static function checkeMail($username,$email){
        $rt=self::field(true)->where('username',$username)->where('email',$email)->find();
        return $rt;
    }


    public static function getsignature($userid,$type='',$salt='zuoxiang'){
        try{
            $userObj=self::get($userid);
            if(empty($userObj)){
                return false;
            }
            $signatrue="";
            $password=md5($userObj->password);
            $reg_time=md5($userObj->getData('reg_time'));

            $signatrue.=substr($password,6,10);
            $signatrue.=substr($reg_time,6,5);

            if(!empty($salt)){
                $salt=md5($salt);
                $signatrue.=substr($salt,6,10);
            }

            if('reset'==$type){
                $reset_time=$userObj->getData('reset_time');
                if(empty($reset_time)){
                 return false;
                }
                $reset_time=md5($userObj->getData('reset_time'));
                $signatrue.=substr($reset_time,5,5);

            }

            return $signatrue;
        }catch(\Exception $e){
            return false;
        }
    }

    public static function getcode($userid,$type='',$salt='zhuoxiang'){
        if(empty($userid)) return false;

        $signature=self::getsignature($userid,$type,$salt);
        if(empty($signature)) return false;
        return encode($userid.','.$signature);
    }

    public static function checkCode($code,$type = '',$salt = "zhuoxiang"){

        $code = decode($code);

        if(empty($code) or !strpos($code,',')){
            return false;
        }

        list($userid,$signatureCookie) = explode(',',$code,2);

        $signature = self::getSignature($userid,$type,$salt);

        if($signature === $signatureCookie){
            return self::getUser(null,$userid);
        }else{
            return false;
        }
    }




 public static function setTime($userid,$type='reset'){
        try{
            $model=new static();

            if('reset'==$type){
                $field='reset_time';
            }else{
                $field='reg_time';
            }

            return $model->update([
                'id'   => $userid,
                $field => time(),
            ]);

        }catch(\Exception $e){
            return false;
        }

 }









}